import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

// 账号模块
export const useAccountStore = defineStore(
  'big-account',
  () => {
    const account = ref({
      username: '',
      token: '',
      role: ''
    })

    // 计算属性方便访问单个值
    const role = computed(() => account.value.role)
    const token = computed(() => account.value.token)

    // 统一设置用户信息
    const setAccount = data => {
      account.value = { ...account.value, ...data }
    }

    // 清除用户信息
    const clearAccount = () => {
      account.value = {
        username: '',
        token: '',
        role: ''
      }
    }

    return {
      account,
      role,
      token,
      setAccount,
      clearAccount
    }
  },
  {
    // 持久化
    persist: true
  }
)
